<?php

namespace App\Http\Controllers\Web\Seller;

use App\Repository\Repositories\Interfaces\CompanyRepository;
use App\Repository\Repositories\Interfaces\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Web\BaseController;
use App\ConstDir\ErrorConst;
use App\Repository\Repositories\Interfaces\OrdersRepository;


/**
 * 企业认证相关
 *
 * @package App\Http\Controllers\Backend
 */
class CompanyController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    protected $company_repo;

    public function __construct(CompanyRepository $company_repo )
    {
        $this->company = $company_repo;
    }


    public function index()
    {


    }


    public function show($id)
    {

    }


    public function saveCompany(UserRepository $user_repo,Request $request)
    {
        $this->validate($request,[
            'company_name' =>'required|string',
            'angent' => 'required|sometimes|string',
            'reg_location' => 'required|sometimes|string',
            'company_address' => 'required|sometimes|string',
            'company_tel' => 'required|sometimes|string',
            'bank' => 'required|sometimes|string',
            'bank_account' => 'required|sometimes|string',
            'licence_type' => 'required|sometimes|string',
            'agnet_licence' => 'required|sometimes|string',
            //'tax_certificate' => 'required|sometimes|string',
            //'organization_code_certificate' => 'required|sometimes|string',
        ]);
        $data = $request->all();
        $data['user_id'] = $data['userid'];
        $data['status'] = 'uncheck';
        $data['apply_at'] = date("Y-m-d H:i:s",time());
        $data['step'] = 1;
        $data['pay_money_num'] = '0.'.rand(10,99);
        $user_parm = [
            'identification_type'=>1
        ];

        $company = $this->company->checkCompany($data['userid']);
        if(!empty($company)){
            $ret = $this->company->update($data,$company['id']);
        }else{
            $ret = $this->company->create($data);
        }
        $user_ret = $user_repo->update($user_parm,$data['userid']);
        if($ret){
            return $this->success(ErrorConst::SUCCESS_CODE_MSG, $ret);
        }else{
            return $this->error(ErrorConst::FAILED_CODE_MSG);
        }

    }


    /*
     * 企业认证第二步打款金额验证
     */
    public function checkCompanyMoney(UserRepository $user_repo,Request $request)
    {
        $this->validate($request,[
            'company_id' =>'required|string',
            'money' => 'required|string',
        ]);
        $data = $request->all();

        $company = $this->company->checkCompany($data['userid']);

        if(!empty($company) and $company['step'] == 2 and $company['pay_money_num'] == $data['money']){
            $parm = [
                'step'=>4,
                'status'=>'pass'
            ];
            $user_parm = [
                'identification_type'=>2
            ];
            $ret = $this->company->update($parm,$company['id']);
            $user_ret = $user_repo->update($user_parm,$company['user_id']);

            return $this->success(ErrorConst::SUCCESS_CODE_MSG,$ret);
        }else{
            return $this->error(ErrorConst::COMPANY_MONEY_ERROR_MSG);
        }


    }

    public function summery(OrdersRepository $orders_repo,Request $request){
        $user =   Auth::check() ? Auth::user() :'';
        $shop_id = array_get($user, 'shop_id');
        //$unreceive_money = $orders_repo->sum('order_money',['pay_status'=>0,'receivable_type'=>1,'shop_id'=>$shop_id]);
        $unreceive_money = $orders_repo->sum('order_money',['pay_status'=>1,'receivable_type'=>1,'shop_id'=>$shop_id]);
        $receive_money = $orders_repo->sum('order_money',['pay_status'=>1,'receivable_type'=>2,'shop_id'=>$shop_id]);
        $data['unreceive_money'] = sprintf("%.2f",$unreceive_money);
        $data['receive_money'] = sprintf("%.2f",$receive_money);
        $data['total_money'] = sprintf("%.2f",$receive_money+$unreceive_money);
        return $this->success(ErrorConst::SUCCESS_CODE_MSG, $data);
    }



}
